package com.common.test;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import com.common.test.InterfaceTestBase;
import com.common.util.ExcelUtil;

	/**
		@author 王嘉犇
		@name 初始化数据驱动表以及结果报表
	 */

public class InitParameterExcel {

	InterfaceTestBase tb;
	
	protected Logger log = Logger.getLogger(InitParameterExcel.class);
	
	public InitParameterExcel(InterfaceTestBase tb){
		this.tb = tb;
	}
	
	/**
	 * 参数表有效行数对应id列设置为从1开始自增的int型数字
	 */
	public void setparamfileidcolumnnumric(String sheetName)  throws Exception{
		int rownum = getParamRownum(sheetName);
		for(int i=1;i<rownum;i++){
			if(ExcelUtil.getCellValue(tb.getDataDriverFilePath(), sheetName, i, "id").equals(String.valueOf(i))){
				continue;
			}else{
				ExcelUtil.writeExcel(tb.getDataDriverFilePath(), sheetName, i, "id", String.valueOf(i));
			}
		}
	}
	
	/**
	 * 理财计划导入业务name列生成序列（导入数据在最多四位数）
	 */
	public void setnewnameofimportloanservice(String sheetName) throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
        Calendar cal = Calendar.getInstance();
        Date date = cal.getTime();
        String datestr = sdf.format(date);
        Random random = new Random();
        int x = random.nextInt(9999);
        String tempstr = datestr + String.valueOf(x);
        int rownum = getParamRownum(sheetName);
		for(int i=1;i<rownum;i++){
			if(i<10){
				ExcelUtil.writeExcel(tb.getDataDriverFilePath(), sheetName, i, "name", "512" + tempstr + "000" + String.valueOf(i));
				ExcelUtil.writeExcel(tb.getDataDriverFilePath(), sheetName, i, "contractNo", "512" + tempstr + "000" + String.valueOf(i));
			}else if(i>=10&&i<100){
				ExcelUtil.writeExcel(tb.getDataDriverFilePath(), sheetName, i, "name", "512" + tempstr + "00" + String.valueOf(i));
				ExcelUtil.writeExcel(tb.getDataDriverFilePath(), sheetName, i, "contractNo", "512" + tempstr + "00" + String.valueOf(i));
			}else if(i>=100&&i<1000){
				ExcelUtil.writeExcel(tb.getDataDriverFilePath(), sheetName, i, "name", "512" + tempstr + "0" + String.valueOf(i));
				ExcelUtil.writeExcel(tb.getDataDriverFilePath(), sheetName, i, "contractNo", "512" + tempstr + "0" + String.valueOf(i));
			}else{
				ExcelUtil.writeExcel(tb.getDataDriverFilePath(), sheetName, i, "name", "512" + tempstr + String.valueOf(i));
				ExcelUtil.writeExcel(tb.getDataDriverFilePath(), sheetName, i, "contractNo", "512" + tempstr + String.valueOf(i));
			}
		}
	}
	
	/**
	 * 测试前清理测试结果result表
	 */
    public void cleanresultsheet(String sheetName) throws IOException{
    	String resultfilepath = tb.getTestResultFilePath();
    	int numberOfRows = getResultRownum(sheetName);
    	int numberOfColumns = getResultColumnnum(sheetName);
    	log.info("numberOfRows is " + numberOfRows);
    	if(numberOfRows>1){
        	for(int rowNum=1;rowNum<numberOfRows;rowNum++){
        		for(int columnNum=0;columnNum<numberOfColumns;columnNum++){
        			ExcelUtil.writeExcel(resultfilepath, sheetName, rowNum, columnNum, "");
        		}
        	}
    	}
    }
    
    /**
     * 取得datadriver参数表的有效行数
     */
    private int getParamRownum(String sheetName) throws IOException{
    	String paramfilepath = tb.getDataDriverFilePath();
    	InputStream is = new FileInputStream(paramfilepath);
    	HSSFWorkbook workbook = new HSSFWorkbook(is);
    	Sheet sheet = workbook.getSheet(sheetName);
    	int numberOfRows = sheet.getLastRowNum() + 1;
    	workbook.close();
    	is.close();
    	return numberOfRows;
    }
	
    /**
     * 取得结果列表的有效行数
     */
    private int getResultRownum(String sheetName) throws IOException{
    	String resultfilepath = tb.getTestResultFilePath();
    	InputStream is = new FileInputStream(resultfilepath);
    	HSSFWorkbook workbook = new HSSFWorkbook(is);
    	Sheet sheet = workbook.getSheet(sheetName);
    	int numberOfRows = sheet.getLastRowNum() + 1;
    	workbook.close();
    	is.close();
    	return numberOfRows;
    }
    
    /**
     * 取得结果列表的有效列数
     */
    private int getResultColumnnum(String sheetName) throws IOException{
    	String resultfilepath = tb.getTestResultFilePath();
    	InputStream is = new FileInputStream(resultfilepath);
    	HSSFWorkbook workbook = new HSSFWorkbook(is);
    	Sheet sheet = workbook.getSheet(sheetName);
    	int numberOfColumns = countNonEmptyColumns(sheet);
    	workbook.close();
    	is.close();
    	return numberOfColumns;
    }
    
    /**
     * 统计第一行有效列数
     */
    private int countNonEmptyColumns(Sheet sheet) {
        // TODO Auto-generated method stub
        Row firstRow = sheet.getRow(0);
        return firstEmptyCellPosition(firstRow);
    }
    
    /**
     * 得到某行最后一个非空的列数
     */
    private int firstEmptyCellPosition(Row cells) {
        // TODO Auto-generated method stub
        int columnCount = 0;
        for (Cell cell : cells) {
            if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
                break;
            }
            columnCount++;
        }
        return columnCount;
    }
}
